package com.wanmait.baohan.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.wanmait.baohan.mapper.DailyPlanMapper;
import com.wanmait.baohan.mapper.DelPlanMapper;
import com.wanmait.baohan.mapper.PlanPicMapper;
import com.wanmait.baohan.pojo.DailyPlan;
import com.wanmait.baohan.pojo.DelPlan;
import com.wanmait.baohan.pojo.PlanPic;
import com.wanmait.baohan.pojo.Staff;
import com.wanmait.baohan.service.DailyPlanService;
import com.wanmait.baohan.util.GetDay;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class DailyPlanServiceImpl  implements DailyPlanService {
    @Resource
    private DailyPlanMapper dailyPlanMapper;

    @Resource
    private PlanPicMapper planPicMapper;

    @Resource
    private DelPlanMapper delPlanMapper;
    @Override
    public void insertPlan(DailyPlan dailyPlan) {
        Date date =new Date();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String format = simpleDateFormat.format(date);
        try {
            date = simpleDateFormat.parse(format);
            int a = date.compareTo(dailyPlan.getStartTime());

            if(a==0){
                dailyPlan.setEnable(1);
            }else if (a==-1){
                dailyPlan.setEnable(0);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        dailyPlanMapper.insertPlan(dailyPlan);
    }

    //找到一个人所有计划
    @Override
    public List<DailyPlan> findList(Integer id) {

        return dailyPlanMapper.findList(id);
    }
    //找到一个人所选月份的全部计划
    @Override
    public List<DailyPlan> findEnableList(Integer id, Integer year,Integer month) {

        GetDay getDay =new GetDay();
        int days = getDay.getDays(year,month);

        String startTime = year+"-"+month+"-01 00:00:00";
        String endTime = year+"-"+month+"-"+days+" 00:00:00";


        Date sdate=null;
        Date edate=null;
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            sdate = simpleDateFormat.parse(startTime );
            edate = simpleDateFormat.parse(endTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dailyPlanMapper.findEnableList(id,sdate,edate);
    }
    //获得哪一天的全部计划
    @Override
    public PageInfo<DailyPlan>findDayList(String date,Integer id,Integer page,Integer limit,Integer enabl)  {

        Date date1 =null;

        if("null".equals(date)||date==null){
            date1 =new Date();
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            String format = simpleDateFormat.format(date1);
            try {
                date1 =simpleDateFormat.parse(format);

            } catch (ParseException e) {
                e.printStackTrace();

            }
        }else {
            try {
                date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);

            } catch (ParseException e) {
                e.printStackTrace();

            }

        }


        PageHelper.startPage(page,limit);
        List<DailyPlan> dayList = dailyPlanMapper.findDayList(id, date1,enabl);

        PageInfo<DailyPlan> dailyPlanPageInfo =new PageInfo<>(dayList);
        return dailyPlanPageInfo ;
    }

    //删除计划
    @Override
    public void delPlan(Integer id, String value, Staff staff) {
        DelPlan delPlan =new DelPlan();
        delPlan.setStaff(staff);
        delPlan.setReason(value);
        delPlan.setDailyPlanId(id);

        delPlanMapper.insertSelective(delPlan);
        dailyPlanMapper.delPlan(id);
    }
    //完成计划
    @Override
    public void comletePlan(Integer id) {
        dailyPlanMapper.comPlan(id);
    }
    //修改计划内容
    @Override
    public void changePlan(Integer id, String work) {

        dailyPlanMapper.changePlan(id,work);
    }


    //定时任务，修改工作的enable
    @Override
    public void changeEnable() {
        Date date =new Date();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String format = simpleDateFormat.format(date);
        try {
            date= simpleDateFormat.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        dailyPlanMapper.changeEable1(date);
        dailyPlanMapper.changeEable2(date);
    }
    //找到安排的工作
    @Override
    public PageInfo<DailyPlan> findArrangePlan(String date, Integer page, Integer limit, Integer enable, Integer id) {


        Date da =null;
        if (date==null){
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            da= new Date();
            String format =simpleDateFormat.format(da);
            try {
                da = simpleDateFormat.parse(format);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
            try {
                da=simpleDateFormat.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        PageHelper.startPage(page,limit);
        List<DailyPlan> arrangeWorks = dailyPlanMapper.findArrangeWork(da, enable, id);

        PageInfo<DailyPlan> planPageInfo= new PageInfo<>(arrangeWorks);

        return planPageInfo;
    }

    //完成任务后 添加图片
    @Override
    public void inserPlanPic(MultipartFile[] multipartFiles, Integer id) {
        PlanPic planPic =null;
        DailyPlan dailyPlan =new DailyPlan();
        dailyPlan.setId(id);

        for (MultipartFile multipartFile : multipartFiles) {

            if(!multipartFile.isEmpty()) {
                String ext = FilenameUtils.getExtension(multipartFile.getOriginalFilename());

                if (",jpg,png,gif,bmp,jpeg,webp,".contains("," + ext + ",")) {

                try {
                    //唯一文件名
                    String fileName = UUID.randomUUID() + "." + ext;
                    //物理路径
                    String path = ResourceUtils.getURL("classpath:").getPath()+"static/image/planPictures";//request.getServletContext().getRealPath("/static/image");

                    File file = new File(path, fileName);
                    multipartFile.transferTo(file);
                    planPic =new PlanPic();

                    planPic.setDailyPlan(dailyPlan);
                    planPic.setPic(fileName);

                    planPicMapper.insert(planPic);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            }
        }
        this.comletePlan(id);
    }

    //通过id获得完成任务的图片存档
    @Override
    public List<PlanPic> findPlanPicturesById(Integer id) {
        List<PlanPic> planPics = planPicMapper.findListByid(id);
        return planPics;
    }
    //删除的计划
    @Override
    public DelPlan findDelplanDetail(Integer id) {
        DelPlan delPlan = delPlanMapper.selectByPrimaryKey(id);
        return delPlan;
    }

    @Override
    public void insert(Object object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(Object object) {

    }


    @Override
    public DailyPlan findById(Integer id) {
        return null;
    }

    @Override
    public List<DailyPlan> findAll() {
        return null;
    }

    @Override
    public List findAll(Object object) {
        return null;
    }


    @Override
    public PageInfo<DailyPlan> findPage(int pageNum) {
        return null;
    }

    @Override
    public PageInfo<DailyPlan> findPage() {
        return null;
    }

    @Override
    public PageInfo findPage(int pageNum, Object object) {
        return null;
    }

    @Override
    public PageInfo findPage(Object object) {
        return null;
    }


}
